package com.ss.android.common.util;

import android.annotation.SuppressLint;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import com.bytedance.common.utility.Logger;
import com.ss.android.common.app.AppHooks;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MultiProcessSharedProvider extends ContentProvider {
    public static final class a {
        private Context a;
        private ContentValues b;

        public a(Context arg1, byte arg2) {
            this(arg1);
        }

        private a(Context arg2) {

            this.b = new ContentValues();
            this.a = arg2.getApplicationContext();
        }

        public final a a(String arg2, String arg3) {
            this.b.put(arg2, arg3);
            return this;
        }

        public synchronized final void a() {
            this.b();
        }

        public final a a(String arg3, float arg4) {
            this.b.put(arg3, Float.valueOf(arg4));
            return this;
        }

        public final a a(String arg3, int arg4) {
            this.b.put(arg3, Integer.valueOf(arg4));
            return this;
        }

        public final a a(String arg3, long arg4) {
            this.b.put(arg3, Long.valueOf(arg4));
            return this;
        }

        public final a a(String arg3, boolean arg4) {
            this.b.put(arg3, Boolean.valueOf(arg4));
            return this;
        }

        private synchronized void b() {
            Uri uri = MultiProcessSharedProvider.getContentUri(this.a, "key", "type");
            ContentResolver resolver = this.a.getContentResolver();
            resolver.insert(uri, this.b);
        }
    }

    public static final class MultiProcessSharedPreference {
        public Context mContext;

        private MultiProcessSharedPreference(Context context) {
            this.mContext = context.getApplicationContext();
            if (Logger.debug()) {
                Logger.d("PushService", "MultiProcessShared create");
            }
        }

        public final int getInt(String key, int fallback) {
            try {
                Uri uri = MultiProcessSharedProvider.getContentUri(this.mContext, key, "integer");
                Cursor cursor = this.mContext.getContentResolver().query(uri, null, null, null, null);
                return MultiProcessSharedProvider.getInt(cursor, fallback);
            } catch (Throwable throwable) {
                return fallback;
            }
        }

        public final String getString(String key, String fallback) {
            try {
                Uri uri = MultiProcessSharedProvider.getContentUri(this.mContext, key, "string");
                Cursor cursor = this.mContext.getContentResolver().query(uri, null, null, null, null);
                return MultiProcessSharedProvider.getString(cursor, fallback);
            } catch (Throwable throwable) {
                return fallback;
            }
        }
    }

    private static String sAuthority;
    private static Uri sBaseUri;
    private static UriMatcher sUriMatcher;
    private SharedPreferences mSharedPreferences;
    private Map e = new ConcurrentHashMap();
    private static MultiProcessSharedPreference mMultiProcessSharedPreference;

    static int getInt(Cursor cursor, int fallback) {
        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    return cursor.getInt(0);
                }
            } catch (Throwable throwable) {
            } finally {
                if (cursor != null) {
                    try {
                        cursor.close();
                    } catch (Exception e) {
                    }
                }
            }
        }

        return fallback;
    }

    private synchronized SharedPreferences getSharedPreference() {
        if (this.mSharedPreferences == null) {
            Context context = this.getContext().getApplicationContext();
            this.mSharedPreferences = context.getSharedPreferences("multi_process_config",
                    Build.VERSION.SDK_INT >= 11 ? Context.MODE_MULTI_PROCESS : Context.MODE_PRIVATE);
            return this.mSharedPreferences;
        } else {
            return this.mSharedPreferences;
        }
    }

    public static synchronized final Uri getContentUri(Context context, String key, String type) {
        if (MultiProcessSharedProvider.sBaseUri == null) {
            try {
                if (Logger.debug()) {
                    Logger.d("MultiProcessSharedProvider", "init form getContentUri");
                }
                MultiProcessSharedProvider.initBaseUri(context);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return MultiProcessSharedProvider.sBaseUri.buildUpon().appendPath(key).appendPath(type).build();
    }

    public static a a(Context arg2) {
        return new a(arg2, (byte) 0);
    }

    static String getString(Cursor arg2, String arg3) {
        if (arg2 != null) {
            try {
                if (arg2.moveToFirst()) {
                    return arg2.getString(0);
                }
            } catch (Throwable v0) {

            } finally {
                if (arg2 != null) {
                    try {
                        arg2.close();
                    } catch (Exception v1) {
                    }
                }
            }
        }

        return arg3;
    }

    private void a(Uri arg3) {
        this.getContext().getContentResolver().notifyChange(arg3, null);
    }

    public synchronized static MultiProcessSharedPreference getMultiProcessSharedPreference(Context context) {
        MultiProcessSharedPreference v0_1;
        if (MultiProcessSharedProvider.mMultiProcessSharedPreference == null) {
            MultiProcessSharedProvider.mMultiProcessSharedPreference = new MultiProcessSharedPreference(context);
        }

        v0_1 = MultiProcessSharedProvider.mMultiProcessSharedPreference;
        return v0_1;
    }

    private static void initBaseUri(Context context) throws IllegalStateException {
        String providerAuthority = ToolUtils.getProviderAuthority(context, MultiProcessSharedProvider.class.getName());
        MultiProcessSharedProvider.sAuthority = providerAuthority;
        if (TextUtils.isEmpty(providerAuthority)) {
            throw new IllegalStateException("Must Set MultiProcessSharedProvider Authority");
        }
        if (Logger.debug()) {
            Logger.d("MultiProcessSharedProvider", MultiProcessSharedProvider.sAuthority);
        }

        UriMatcher uriMatcher = new UriMatcher(-1);
        MultiProcessSharedProvider.sUriMatcher = uriMatcher;
        uriMatcher.addURI(MultiProcessSharedProvider.sAuthority, "*/*", 65536);
        MultiProcessSharedProvider.sBaseUri = Uri.parse("content://" + MultiProcessSharedProvider.sAuthority);
    }

    public int delete(Uri arg4, String arg5, String[] arg6) {
        switch (MultiProcessSharedProvider.sUriMatcher.match(arg4)) {
            case 65536: {
                break;
            }
            default:
                throw new IllegalArgumentException("Unsupported uri " + arg4);
        }


        try {
            this.getSharedPreference().edit().clear().commit();
            this.e.clear();
            this.a(MultiProcessSharedProvider.getContentUri(this.getContext(), "key", "type"));
        } catch (Exception v0) {
        }

        return 0;
    }

    public String getType(Uri uri) {
        return "vnd.android.cursor.item/vnd." + MultiProcessSharedProvider.sAuthority + ".item";
    }

    @SuppressLint(value = {"NewApi"})
    public Uri insert(Uri arg11, ContentValues arg12) {
        SharedPreferences.Editor v2_2;
        Object value;
        Object key;
        SharedPreferences.Editor v4;
        Uri v5 = null;
        switch (MultiProcessSharedProvider.sUriMatcher.match(arg11)) {
            case 65536: {
                break;
            }
            default:
                throw new IllegalArgumentException("Unsupported uri " + arg11);
        }


        try {
            Iterator v6 = arg12.valueSet().iterator();
            v4 = ((SharedPreferences.Editor) v5);
            while (true) {
                if (!v6.hasNext()) {
                    break;
                }

                key = v6.next();
                value = ((Map.Entry) key).getValue();
                key = ((Map.Entry) key).getKey();
                if (Logger.debug()) {
                    Logger.d("PushService", "MultiProcessShareProvider insert key = " + (((String) key)) + " value = " + value.toString());
                }

                int v2 = 0;
                if (value == null) {
                    this.e.remove(key);
                    v2 = 1;
                } else {
                    Object v7 = this.e.get(key);
                    if (v7 != null && (v7.equals(value))) {
                    } else {
                        this.e.put(key, value);
                        v2 = 1;
                    }
                }

                if (v2 == 0) {
                    continue;
                }

                if (Logger.debug()) {
                    String v7_1 = "PushService";
                    String v2_1 = "MultiProcessShareProvider reallly insert key = " + key + " value = " + value != null ? value.toString() : "null";
                    Logger.d(v7_1, v2_1);
                }

                if (v4 == null) {
                    v2_2 = this.getSharedPreference().edit();
                } else {
                    v2_2 = v4;
                }

                if (value == null) {
                    v2_2.remove(((String) key));
                    v4 = v2_2;
                    continue;
                } else if ((value instanceof String)) {
                    v2_2.putString(((String) key), ((String) value));
                    this.a(MultiProcessSharedProvider.getContentUri(this.getContext(), ((String) key), "string"));
                    v4 = v2_2;
                    continue;
                } else if ((value instanceof Boolean)) {
                    v2_2.putBoolean(((String) key), ((Boolean) value).booleanValue());
                    this.a(MultiProcessSharedProvider.getContentUri(this.getContext(), ((String) key), "boolean"));
                    v4 = v2_2;
                    continue;
                } else if ((value instanceof Long)) {
                    v2_2.putLong(((String) key), ((Long) value).longValue());
                    this.a(MultiProcessSharedProvider.getContentUri(this.getContext(), ((String) key), "long"));
                    v4 = v2_2;
                    continue;
                } else if ((value instanceof Integer)) {
                    v2_2.putInt(((String) key), ((Integer) value).intValue());
                    this.a(MultiProcessSharedProvider.getContentUri(this.getContext(), ((String) key), "integer"));
                    v4 = v2_2;
                    continue;
                } else if ((value instanceof Float)) {
                    v2_2.putFloat(((String) key), ((Float) value).floatValue());
                    this.a(MultiProcessSharedProvider.getContentUri(this.getContext(), ((String) key), "float"));
                    v4 = v2_2;
                    continue;
                } else {

                    throw new IllegalArgumentException("Unsupported type " + arg11);
                }
            }
        } catch (Exception v0) {
            return v5;
        }


        try {
            if (v4 != null) {
                v4.apply();
            }

            return v5;
        } catch (Exception v0) {
        }

        return v5;
    }

    public boolean onCreate() {
        if (MultiProcessSharedProvider.sUriMatcher == null) {
            try {
                if (Logger.debug()) {
                    Logger.d("MultiProcessSharedProvider", "init form onCreate");
                }

                MultiProcessSharedProvider.initBaseUri(this.getContext());
                SharedPreferences v0_1 = this.getSharedPreference();
                if (v0_1 == null) {

                } else {

                    Iterator v2 = v0_1.getAll().entrySet().iterator();
                    while (v2.hasNext()) {
                        Object v0_2 = v2.next();
                        this.e.put(((Map.Entry) v0_2).getKey(), ((Map.Entry) v0_2).getValue());
                    }
                }
            } catch (Exception v0) {
                v0.printStackTrace();
                boolean v0_5 = false;
                return v0_5;
            }
        }

        try {
            AppHooks.InitHook v0_4 = AppHooks.getInitHook();
            if (v0_4 != null) {
                v0_4.tryInit(this.getContext());
                return true;
            }

            this.getContext().startService(new Intent(this.getContext(), Class.forName("com.ss.android.newmedia.message.MessageHandler")));
            return true;
        } catch (Throwable v0_3) {
            return true;
        }
    }

    public Cursor query(Uri arg11, String[] arg12, String arg13, String[] arg14, String arg15) {
        Integer v5_3;
        String v9;
        String v0_5;
        Object v5_2;
        String v5_1;
        MatrixCursor.RowBuilder v7;
        MatrixCursor v0_4;
        int v1_2;
        MatrixCursor.RowBuilder v5;
        Object v1_1;
        Cursor v0_3;
        Object v0_2;
        Iterator v6;
        MatrixCursor v2_1;
        Map v0_1;
        Cursor v1 = null;
        switch (MultiProcessSharedProvider.sUriMatcher.match(arg11)) {
            case 65536: {
                break;
            }
            default:
                throw new IllegalArgumentException("Unsupported uri " + arg11);
        }


        String v2 = "all";
        try {
            if (!v2.equals(arg11.getPathSegments().get(1))) {
                v0_2 = arg11.getPathSegments().get(0);
                boolean v2_2 = this.e.containsKey(v0_2);
                if (!v2_2) {
                    v0_3 = v1;
                    return v0_3;
                }

                v2_1 = new MatrixCursor(new String[]{(String) v0_2});
                v1_1 = this.e.get(v0_2);
                v5 = v2_1.newRow();
                if ((v1_1 instanceof Boolean)) {
                    if (((Boolean) v1_1).booleanValue()) {
                        v1_2 = 1;
                    } else {
                        v1_2 = 0;
                    }

                    v1_1 = Integer.valueOf(v1_2);
                }

                if (Logger.debug()) {
                    Logger.d("PushService", "MultiProcessShareProvider reallly get key = " + (((String) v0_2)) + " value = " + v1_1);
                }

                v5.add(v1_1);
                v0_4 = v2_1;
                return v0_4;
            } else {
                v0_1 = this.getSharedPreference().getAll();
                v2_1 = new MatrixCursor(new String[]{"key_column", "value_column", "type_column"});
                v6 = v0_1.entrySet().iterator();
                while (true) {
                    if (!v6.hasNext()) {
                        v0_4 = v2_1;
                        return v0_4;
                    }
                    v0_2 = v6.next();
                    v1_1 = ((Map.Entry) v0_2).getKey();
                    v0_2 = ((Map.Entry) v0_2).getValue();
                    v7 = v2_1.newRow();
                    v5_1 = "string";
                    if ((v0_2 instanceof String)) {
                        v5_2 = v0_2;
                        v0_5 = "string";
                    } else if ((v0_2 instanceof Boolean)) {
                        v5_1 = "boolean";
                        int v0_6 = ((Boolean) v0_2).booleanValue() ? 1 : 0;
                        v9 = v5_1;
                        v5_2 = Integer.valueOf(v0_6);
                        v0_5 = v9;
                    } else {
                        if ((v0_2 instanceof Integer)) {
                            v5_2 = v0_2;
                            v0_5 = "integer";
                        } else if ((v0_2 instanceof Long)) {
                            v5_2 = v0_2;
                            v0_5 = "long";
                        } else {
                            if (!(v0_2 instanceof Float)) {
                                v9 = v5_1;
                                v5_2 = v0_2;
                                v0_5 = v9;
                            } else {
                                v5_2 = v0_2;
                                v0_5 = "float";
                            }
                        }
                    }
                    v7.add(v1_1);
                    v7.add(v5_2);
                    v7.add(v0_5);
                    continue;
                }
            }
        } catch (Exception v0) {
            return null;
        }
    }

    public int update(Uri arg2, ContentValues arg3, String arg4, String[] arg5) {
        throw new UnsupportedOperationException();
    }
}

